package com.project.nimsen.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.WxinfoDto;
import com.project.nimsen.entity.Wxinfo;
import com.project.nimsen.entity.Wxsendmsg;
import com.project.nimsen.service.IAuthorityService;
import com.project.nimsen.service.IColtypeService;
import com.project.nimsen.service.IWxinfoService;
import com.project.nimsen.service.IWxsendmsgService;
import com.project.nimsen.weixin.common.WeiXinConstant;
import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
@Repository("wxinfoService")
public class WxinfoServiceImpl extends BaseServiceImpl implements
		IWxinfoService {
	@Resource
	IBaseDao baseDao;
	@Resource
	IColtypeService coltypeService;
	@Resource
	IAuthorityService authorService;
	@Resource
	IWxsendmsgService wxsendmsgService;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public Wxinfo findByid(String wxid) throws Exception {
		if(StringUtils.isBlank(wxid))
			return null;
		Finder finder = new Finder("select id,wxusername,wxoldid,weixinumber,siteid,appid,appsecret,accesstoken,accesstokentime,createUser,createTime,active from wxinfo where active!=1 and id=:wxid");
		finder.setParam("wxid", wxid);
		return baseDao.queryForObject(finder, Wxinfo.class);
	}
	public Wxinfo findBysid(String siteid) throws Exception {
		if(StringUtils.isBlank(siteid))
			return null;
		Finder finder = new Finder("select id,wxusername,wxoldid,weixinumber,siteid,appid,appsecret,accesstoken,accesstokentime,createUser,createTime,active from wxinfo where active!=1 and siteid=:siteid");
		finder.setParam("siteid", siteid);
		return baseDao.queryForObject(finder, Wxinfo.class);
	}
	public boolean saveByDto(WxinfoDto dto) throws Exception {
		Wxinfo info = dto.getInfo();
		if(StringUtils.isBlank(info.getId()))
			baseDao.saveEntity(info);
		else
			baseDao.update(info);
		if(StringUtils.isBlank(dto.getDefaultkeword()))
			return true;
		Wxsendmsg targetmsg = wxsendmsgService.findByKeyWord(dto.getDefaultkeword(), info.getSiteid());
		if(targetmsg==null)
			return false;
		Wxsendmsg defaultmsg = wxsendmsgService.findByKeyWord(WeiXinConstant.WEIXIN_MSGTYPE_DEFAULT, info.getSiteid());
		if(defaultmsg==null){
			defaultmsg = new Wxsendmsg();
			defaultmsg.setActive(0);
			defaultmsg.setCreateTime(new Date());
			defaultmsg.setCreateUser(info.getCreateUser());
			defaultmsg.setMsgtype(WeiXinConstant.WEIXIN_MSGTYPE_DEFAULT);
			defaultmsg.setSiteid(info.getSiteid());
			defaultmsg.setTargetid(targetmsg.getId());
			defaultmsg.setKeyword(WeiXinConstant.WEIXIN_MSGTYPE_DEFAULT);
			baseDao.saveEntity(defaultmsg);
		}else
			defaultmsg.setTargetid(targetmsg.getId());
		baseDao.update(defaultmsg);
		return true;
	}

}
